package com.oj.system.service.user.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oj.common.core.domain.TableDataInfo;
import com.oj.common.core.enums.ResultCode;
import com.oj.common.security.exception.ServiceExpection;
import com.oj.system.domain.user.User;
import com.oj.system.domain.user.dto.UserQueryDTO;
import com.oj.system.domain.user.dto.UserStatusDTO;
import com.oj.system.domain.user.vo.UserVO;
import com.oj.system.mapper.UserMapper;
import com.oj.system.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author bin
 * @Date 2025/5/22
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public TableDataInfo list(UserQueryDTO userQueryDTO) {
        //UserVO返回
        PageHelper.startPage(userQueryDTO.getPageNum(),userQueryDTO.getPageSize());
        List<UserVO> userVOS = userMapper.selectUserList(userQueryDTO);
        if(CollectionUtil.isEmpty(userVOS)){
            return TableDataInfo.empty();
        }
        return TableDataInfo.success(userVOS, new PageInfo<>(userVOS).getTotal());
    }

    @Override
    public int updateStatus(UserStatusDTO userStatusDTO){
        User user = userMapper.selectById(userStatusDTO.getUserId());
        if(user == null){
            throw new ServiceExpection(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        user.setStatus(userStatusDTO.getStatus());
        return userMapper.updateById(user);
    }

}
